package com.leo.erp.web.systemconfig;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.leo.erp.dto.SystemConfigExecution;
import com.leo.erp.entity.SystemConfig;
import com.leo.erp.entity.User;
import com.leo.erp.enums.SystemConfigStateEnum;
import com.leo.erp.service.SystemConfigService;
import com.leo.erp.util.GlobalRuntimeException;
import com.leo.erp.util.HttpServletRequestUtil;

@RequestMapping(value = "/systemConfig")
@Controller
public class SystemConfigManagementController {
	@Autowired
	private SystemConfigService systemConfigService;
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String companyName = HttpServletRequestUtil.getString(request, "companyName");
		int currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		SystemConfigExecution se = new SystemConfigExecution();
		//调用方法进行分页查询
		se = systemConfigService.getPageList(companyName, currentPage, pageSize);
		if(se.getState() == SystemConfigStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("pages",se.getPages());//总页数
			modelMap.put("total", se.getTotal());//总数
			
			Map<String, Object> map = new HashMap<>();
			map.put("rows", se.getSystemConfigPageList());
			map.put("total", se.getTotal());
			modelMap.put("systemConfigList", map);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/checkCompanyNameExist")
	@ResponseBody
	public Map<String, Object> checkCompanyNameExist(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String companyName = HttpServletRequestUtil.getString(request, "companyName");
		SystemConfigExecution se = systemConfigService.checkIsNameExist(companyName);
		if(se.getState() == SystemConfigStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("flag", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "名称已经存在");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/updateSystemConfig")
	@ResponseBody
	public Map<String, Object> updateSystemConfig(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		int systemConfigId = HttpServletRequestUtil.getInt(request, "id");
		try {
			SystemConfig systemConfig = JSON.parseObject(beanJson, SystemConfig.class);
			//获取session
			User user = (User) request.getSession().getAttribute("user");
			systemConfig.setTenantId(user.getUserId());
			systemConfig.setSystemConfigId(systemConfigId);
			//查看是否有同名
			/*SystemConfigExecution se1 = systemConfigService.checkIsNameExist(systemConfig.getCompanyName());
			if(se1.getState() == SystemConfigStateEnum.SUCCESS.getState()) {
				modelMap.put("success", false);
				modelMap.put("msg", "名称已存在");
			}else {*/
				//更新
				SystemConfigExecution se = systemConfigService.updateSystemConfig(systemConfig);
				if(se.getState() == SystemConfigStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "更新数据异常");
				}
			//}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/addSystemConfig")
	@ResponseBody
	public Map<String, Object> addSystemConfig(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		
		//插入数据
		try {
			SystemConfig systemConfig = JSON.parseObject(beanJson, SystemConfig.class);
			//获取session
			User user = (User) request.getSession().getAttribute("user");
			
			systemConfig.setTenantId(user.getUserId());
			//查看是否有同名
			SystemConfigExecution se1 = systemConfigService.checkIsNameExist(systemConfig.getCompanyName());
			if(se1.getState() == SystemConfigStateEnum.SUCCESS.getState()) {
				modelMap.put("success", false);
				modelMap.put("msg", "名称已存在");
			}else {
				//插入
				SystemConfigExecution se = systemConfigService.addSystemConfig(systemConfig);
				if(se.getState() == SystemConfigStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "插入数据异常");
				}
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/deleteSystemConfig")
	@ResponseBody
	public Map<String, Object> deleteSystemConfig(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int systemConfigId = HttpServletRequestUtil.getInt(request, "ids");
		
		SystemConfigExecution se = systemConfigService.deleteSystemConfig(systemConfigId);
		if(se.getState() == SystemConfigStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "删除信息失败");
		}		
		return modelMap;
	}
}
